<?php

namespace App\Http\Controllers\Admin;

use App\Model\NavigatorModel;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
class NavigatorController extends CommonController

{
    //
    public function index()
    {
        $data = NavigatorModel::all();

        $list =  self::getTree($data,0);

        return view("admin.navigate.index")->with('list',$list);
    }


    // 获取树状结构
    private function getTree($data = array() , $id = 0,$level = 0){

        $list = array();
        foreach ($data as $k => $v) {
            if($v['parent_id'] == $id){
                $v['level'] = $level;
                $v['html'] = str_repeat("--",($level*2));
                $list[$k] = $v;
                $list = array_merge($list,$this->getTree($data,$v['nav_id'],$level+1));
            }
        }
        return $list;
    }



    public function create()
    {
        $parent = NavigatorModel::all();
       return view("admin.navigate.nav_add")->with('parent',$parent);
    }

    public function store(){
       $all = Input::except('_token');

       $rules = array(
         "nav_name" => 'required',
           'nav_url'=>'required'
       );
       $msg = array(

           'nav_name.required' => '导航名不得空!',
           'nav_url'=>'导航链接不能为空!'
       );
       $valid = Validator::make($all,$rules,$msg);
       if($valid->passes()){
           $add = NavigatorModel::create($all);
           if($add){
               return redirect('Admin/navigator');
           }else{
               return back()->with('errors','添加失败!');
           }
       }else{
           return back()->withErrors($valid);
       }
    }

    public function edit($id)
    {

        $parent = NavigatorModel::all();
        $data  = NavigatorModel::find($id);
        return view("admin.navigate.nav_edit")->with('data',$data)->with('parent',$parent);

    }

    public function update($id)
    {
        $all = Input::except('_token','_method');
        $rules = array(
            "nav_name" => 'required',
            'nav_url'=>'required'
        );
        $msg = array(

            'nav_name.required' => '导航名不得空!',
            'nav_url'=>'导航链接不能为空!'
        );
        $valid = Validator::make($all,$rules,$msg);
        if($valid->passes()){
            $add = NavigatorModel::where('nav_id',$id)->update($all);
            if($add){
                return redirect('Admin/navigator');
            }else{
                return back()->with('errors','添加失败!');
            }
        }else{
            return back()->withErrors($valid);
        }
    }

    public function destroy($id)
    {

        $del = NavigatorModel::where('nav_id',$id)->delete();
        $data = array(
            'code'=>0,
            'msg' =>'删除成功'
        );
        if(!$del){
            $data['code'] = 1001;
            $data['msg'] = "删除失败";
        }
        return $data;

    }

    public function navOrder()
    {
        $id = Input::all()['nav_id'];
        $order = Input::all()['nav_order'];
        $data = array(
            'msg'=>'修改失败',
            'code'=>1001
        );
        if($id && $order){
            $sort =  NavigatorModel::where('nav_id',$id)->update(array('sort'=>$order));
            if($sort){
                $data['msg'] = '修改成功';
                $data['code'] = 0;
            }
        }
        return $data;
    }


    public function show()
    {

    }

}
